package com.cs.beneficios.web.action;

import co.gov.medellin.ssti.sub.model.dao.IVerificacionDAO;
import co.gov.medellin.ssti.sub.model.dao.impl.VerificacionDAOImpl;

import com.cs.beneficios.business.entity.VerificacionDAO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.beneficios.web.form.StrutsUploadForm;
import com.cs.paoyer.administration.business.model.UserVO;

import java.io.IOException;

import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class VerificacionBeneficiarioAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String borrar = request.getParameter("borrar");
		if (borrar != null) {
			if (!borrar.equals("")) {
				// ConnectionFactory cf = new ConnectionFactory();
				HttpSession session = request.getSession();
				UserVO user = (UserVO) session.getAttribute("userInfo");
				// VerificacionDAO verificacionDAO = new VerificacionDAO(cf);
				IVerificacionDAO verificacionDAO = new VerificacionDAOImpl();
				// request.setCharacterEncoding("utf-8");
				System.getProperty("file.encoding");

				if (borrar.equals("SI")) {
					try {
						int i = verificacionDAO
								.deleteRecordsOld(user.getName());
						// System.out.println("i: " + i);
					} catch (SQLException e) {
						System.err.println("/******** TDSUB - cwsub");
						e.printStackTrace();
						System.err.println("TDSUB - cwsub ********/");
						request.setAttribute("msg",
								"Error al eliminar los registros anteriormente cargados");
						return mapping.findForward("fail");
					}
				}
				try {
					StrutsUploadForm myForm = (StrutsUploadForm) form;
					FormFile myFile = myForm.getTheFile();
					byte[] fileData = myFile.getFileData();
					verificacionDAO.uploadFile(fileData, user.getName());
					// cf.closeConnection(verificacionDAO.getConn());
				} catch (SQLException e) {
					System.err.println("/******** TDSUB - cwsub");
					e.printStackTrace();
					System.err.println("TDSUB - cwsub ********/");
					request.setAttribute(
							"msg",
							"Error del archivo .csv. Verifique la estructura del archivo cargado y recuerde no incluir el encabezado.");
					return mapping.findForward("fail");
				}
			} else {
				return mapping.findForward("success");
			}
		} else {
			return mapping.findForward("success");
		}
		return mapping.findForward("verificados");
	}
}
